package cn.tx.controller;

import cn.tx.model.LibraryUser;
import cn.tx.service.LibraryUserService;
import cn.tx.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(path = "/libraryUser")
class LibraryUserController {

    @Autowired
    private LibraryUserService libraryUserService;

    /*首页展示*/
    @RequestMapping(path = "/list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("libraryUser/libraryUser_list");
        return modelAndView;
    }


    /*展示用户列表*/
    @RequestMapping(path = "/get_list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getList(
            @RequestParam(value="name",required=false,defaultValue="") String name,
            Page page
    ) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> querymap = new HashMap<String,Object>();
        querymap.put("name","%"+name+"%");
        querymap.put("offset", page.getOffset());
        querymap.put("pageSize", page.getRows());
        map.put("rows",libraryUserService.findList(querymap));
        map.put("total",libraryUserService.findTotal(querymap));
        return map;
    }


    /*添加用户*/
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(LibraryUser libraryUser) {
        Map<String, String> map = new HashMap<>();
        if (libraryUser == null) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }if (StringUtils.isEmpty(libraryUser.getName())) {
            map.put("type", "error");
            map.put("msg", "姓名不能为空");
            return map;
        }
        if (StringUtils.isEmpty(libraryUser.getSex())) {
            map.put("type", "error");
            map.put("msg", "性别不能为空");
            return map;
        }
        if (StringUtils.isEmpty(libraryUser.getBirth())) {
            map.put("type", "error");
            map.put("msg", "出生年月不能为空");
            return map;
        }
        if (StringUtils.isEmpty(libraryUser.getTel())) {
            map.put("type", "error");
            map.put("msg", "联系方式不能为空");
            return map;
        }
        if (StringUtils.isEmpty(libraryUser.getC1ass())) {
            map.put("type", "error");
            map.put("msg", "班级不能为空");
            return map;
        }
        if (StringUtils.isEmpty(libraryUser.getDepartment())) {
            map.put("type", "error");
            map.put("msg", "所属系不能为空");
            return map;
        }
        if (libraryUserService.addLibraryUser(libraryUser) <= 0) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功");
        return map;
    }



    /*编辑用户*/
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(LibraryUser libraryUser) {
        /*Map<String, String> map = new HashMap<>();
        LibraryUser userName = libraryUserService.findByUserName(libraryUser.getName());
        if (userName!=null){
            if (libraryUser.getId()!=userName.getId()){
                map.put("type", "error");
                map.put("msg", "id不匹配");
                return map;
            }
        }*/
        Map<String, String> map = new HashMap<>();
        LibraryUser Name = libraryUserService.findByName(libraryUser.getName());
        if (Name!=null){
            if (libraryUser.getId()!=Name.getId()){
                map.put("type", "error");
                map.put("msg", "id不匹配");
                return map;
            }
        }
        System.out.println(libraryUser);
        if (libraryUserService.UpdateLibraryUserById(libraryUser)<= 0) {
            map.put("type", "error");
            map.put("msg", "修改失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "修改成功");
        return map;
    }


    /*删除用户*/
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value = "ids[]",required = true) Long[] ids
    ) {
        Map<String, String> map = new HashMap<>();
        if (ids==null){
            map.put("type", "error");
            map.put("msg", "请至少选择一条数据");
            return map;
        }
        String idsstring ="";
        for (Long id : ids) {
            idsstring += id +",";
        }
        idsstring = idsstring.substring(0,idsstring.length()-1);
        if (libraryUserService.delete(idsstring)<=0){
            map.put("type", "error");
            map.put("msg", "删除失败");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "删除成功");
        return map;
    }


}
